//
//  AddEquipmentController.m
//  PropertyMananger
//
//  Created by Mansion on 17/2/11.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "AddEquipmentController.h"
#import "AddEquipmentCell.h"
#import "AddInputCell.h"
#import "TextViewCell.h"
#import "ComfirmCell.h"
#import "AddSearchController.h"
#import "AddSearchView.h"
@interface AddEquipmentController ()<SelectedEquipmentDelegate,UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic,strong) UILabel * nameLabel;

@property (nonatomic,strong) AddSearchView * addSearchView;


@property (nonatomic,copy) NSString * equipmentCode;
@property (nonatomic,copy) NSString * equipmentCategory;
@property (nonatomic,copy) NSString * equipmentName;
@property (nonatomic,copy) NSString * equipmentId;

@property (nonatomic,copy) NSString * repairMan;
@property (nonatomic,copy) NSString * repairTime;
@property (nonatomic,copy) NSString * repairContent;
@property (nonatomic,copy) NSString * repairCondition;

@end

@implementation AddEquipmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleViewName:@"新增"];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 50, BHscreenW, BHscreenH - 64 - 50) isGroup:NO];
    self.tableView.separatorStyle = NO;
    
    self.addSearchView = [[AddSearchView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 50)];
    [self.view addSubview:self.addSearchView];
    
    
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapClick:)];
    [self.addSearchView addGestureRecognizer:tap];
    
}
#pragma mark - 点击新增手势
- (void)TapClick:(UITapGestureRecognizer *)tap{
    
    DLog(@"点击图片");
   AddSearchController * addEquipmentVC = [[AddSearchController alloc]init];
    addEquipmentVC.delegate  = self;
   [self.navigationController pushViewController:addEquipmentVC animated:YES];
    
}
#pragma mark - 实现回传代理
- (void)didSelectedEquipment:(NSString *)equipmentCode equipmentCategory:(NSString *)equipmentCategory equipmentName:(NSString *)equipmentName EquipmentId:(NSString *)EquipmentId{

    DLog(@"%@==%@==%@",equipmentCode,equipmentCategory,equipmentName);
    self.equipmentCode = equipmentCode;
    self.equipmentCategory = equipmentCategory;
    self.equipmentName = equipmentName;
    self.equipmentId = EquipmentId;
    
    self.addSearchView.placeholderLabel.text = self.equipmentCode;
    
    [self.tableView reloadData];
    
    
}
- (void)click:(UITapGestureRecognizer *)tap{

    AddSearchController * addSearchVC = [[AddSearchController alloc]init];
    [self.navigationController pushViewController:addSearchVC animated:YES];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 6;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        
        AddEquipmentCell * cell = [AddEquipmentCell cellWithTableView:tableView];
        cell.titleLabel.text = @"设备型号:";
        cell.nameLabel.text = self.equipmentCategory;
        return cell;

    }else if (indexPath.row == 1){
    
        AddEquipmentCell * cell = [AddEquipmentCell cellWithTableView:tableView];
        cell.titleLabel.text = @"设备名称:";
        cell.nameLabel.text = self.equipmentName;
        return cell;

        
    }else if (indexPath.row == 2){
    
        AddInputCell * cell = [AddInputCell cellWithTableView:tableView];
        cell.inputTF.delegate = self;
        cell.titleLabel.text = @"检  修  人:";
        cell.inputTF.placeholder = @"请输入检修人";
        cell.inputTF.delegate = self;
        cell.inputTF.text = userModel.RealName;
        
        return cell;
        
    }else if (indexPath.row == 3){
    
        TextViewCell * cell = [TextViewCell cellWithTableView:tableView];
        cell.titleLabel.textColor = BHColor(102, 102, 102);
        cell.titleLabel.text = @"检修内容";
        cell.textView.placeholder = @"请输入检修内容...";
        cell.textView.delegate = self;
        cell.textView.text = self.repairContent;
        
        return cell;
        
    }else if (indexPath.row == 4){
    
        TextViewCell * cell = [TextViewCell cellWithTableView:tableView];
        cell.titleLabel.textColor = BHColor(102, 102, 102);
        cell.titleLabel.text = @"检修情况";
        cell.textView.placeholder = @"请输入检修情况...";
        cell.textView.delegate = self;
        cell.textView.text = self.repairCondition;

        return cell;
    
    }else if (indexPath.row == 5){
        
        ComfirmCell * cell = [ComfirmCell cellWithTableView:tableView];
        [cell.cancelBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.comfirmBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }
    
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row <= 2) {
        
        return 50;
    }else if (indexPath.row == 5){
    
        return 60;
    }else{
    
     return 150;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 1;
}
#pragma mark - 取消，提交点击事件
- (void)BtnClick:(UIButton *)button{

    if (button.tag == cancelBtnTag) {
        
        DLog(@"点击取消");
        [MBProgressHUD showSuccess:@"取消成功" time:1.5];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else if (button.tag == ComfirmBtnTag){
    
        AddInputCell * repairManCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        [repairManCell.inputTF resignFirstResponder];
        self.repairMan = repairManCell.inputTF.text;
        
        DLog(@"===检修人%@",self.repairMan);
        
        TextViewCell * contentCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        [contentCell.textView resignFirstResponder];
        self.repairContent = contentCell.textView.text;
        
        DLog(@"===检修内容%@",self.repairContent);
        
        TextViewCell * conditonCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
        [conditonCell.textView resignFirstResponder];
        self.repairCondition = conditonCell.textView.text;
        DLog(@"=====检修情况 == %@",self.repairCondition);
        
        if (self.equipmentName == nil) {
            
            [MBProgressHUD showError:@"请输入设备编号" time:1.5];
        }else if ([self.repairContent isEqualToString:@""]){
        
         [MBProgressHUD showError:@"请输入检修内容" time:1.5];
            
        }else if ([self.repairCondition isEqualToString:@""]){
            
          [MBProgressHUD showError:@"请输入检修情况" time:1.5];
        }else{
        
        [self postAddEquipmentData];
            
        }
        DLog(@"点击确定");
        
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField{

    AddInputCell * repairManCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    [repairManCell.inputTF resignFirstResponder];
    self.repairMan = repairManCell.inputTF.text;
    
    DLog(@"===检修人%@",self.repairMan);
}
- (void)textViewDidChange:(UITextView *)textView{

    TextViewCell * contentCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    
    
    self.repairContent = contentCell.textView.text;
    DLog(@"===检修内容%@",self.repairContent);

}
- (void)textViewDidEndEditing:(UITextView *)textView{

    AddInputCell * repairManCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    
    self.repairMan = repairManCell.inputTF.text;

    DLog(@"===检修人%@",self.repairMan);
    
    TextViewCell * contentCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
  
    
    self.repairContent = contentCell.textView.text;
  DLog(@"===检修内容%@",self.repairContent);
    
    TextViewCell * conditonCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
    self.repairCondition = conditonCell.textView.text;
//    DLog(@"=====检修情况 == %@",self.repairCondition);

}

#pragma mark - 数据请求
- (void)postAddEquipmentData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    
    NSDictionary * parms =  @{
                   @"Cid":userModel.CommunityID,
                   @"EquipmentName":self.equipmentName,
                   @"SpecificationModel":self.equipmentCategory,
                   @"MaintenanceMan":self.repairMan,
                   @"RepairTime":@"",
                   @"MaintenanceCSontent":self.repairContent,
                   @"MaintenanceCondition":self.repairCondition,
                   @"EquipmentId":self.equipmentId,
                   @"json":@"\{\"jsonEntity\":\{},\"action\":\"addrepairandcomplaint\",\"version\":\"V1.0\"}"
                   };
    
    DLog(@"提交的参数 === %@",parms);
    
    [HomeNetWorkRequest postAddEquipmentSuccess:^(id data) {
        
        [MBProgressHUD hideHUDForView:self.view];

        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        DLog(@"返回 === %@",dict[@"Code"]);
        DLog(@"返回提示 === %@",dict[@"Msg"]);
        
        if ([[dict[@"Code"]stringValue] isEqualToString:@"1"]) {
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(didRefreshEquipmentRepairToRepair)]) {
            
                [self.delegate didRefreshEquipmentRepairToRepair];
            
            }
            
            [self.navigationController popViewControllerAnimated:YES];
            
             [MBProgressHUD showSuccess:dict[@"Msg"] time:1.5];
        }

        
    } Fail:^(NSString *errorDes) {
        [MBProgressHUD hideHUDForView:self.view];

        [MBProgressHUD hideHUD];
        
    } param:parms ];
    
}



@end
